package com.cskaoyan.service.impl;

import com.cskaoyan.bean.pojo.MallCategory;
import com.cskaoyan.bean.pojo.MallCategoryExample;
import com.cskaoyan.bean.vo.WxCategoryVO;
import com.cskaoyan.mapper.MallCategoryMapper;
import com.cskaoyan.service.WxCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: Fengyi
 * @description:
 * @time: 2021/7/12 10:40
 */
@Service
public class WxCategoryServiceImpl implements WxCategoryService {

    @Autowired
    MallCategoryMapper mallCategoryMapper;

    @Override
    public WxCategoryVO selectCategory(Integer id) {
        WxCategoryVO wxCategoryVO = new WxCategoryVO();

        MallCategoryExample mallCategoryExample = new MallCategoryExample();
        MallCategoryExample.Criteria categoryExampleCriteria = mallCategoryExample.createCriteria();
        categoryExampleCriteria.andDeletedEqualTo(0);



        MallCategory mallCategory = mallCategoryMapper.selectByPrimaryKey(id);
        Integer pid = mallCategory.getPid();


        //setParentCategory
        MallCategory parentMallCategory = null;
        if(pid == 0) {
            parentMallCategory  = mallCategoryMapper.selectByPrimaryKey(id);
        } else {
            parentMallCategory  = mallCategoryMapper.selectByPrimaryKey(pid);
        }
        wxCategoryVO.setParentCategory(parentMallCategory);




        //setCurrentCategory
        if(pid == 0) {
            wxCategoryVO.setCurrentCategory(mallCategory);
        } else {
            wxCategoryVO.setCurrentCategory(mallCategory);
        }


        //修改父类名称为全部
        //mallCategory1.setName("全部");
        //setBrotherCategory
        MallCategory mallCategory1 = null;
        if(pid == 0) {
            categoryExampleCriteria.andPidEqualTo(id);
            mallCategory1 = mallCategoryMapper.selectByPrimaryKey(id);

        } else {
            categoryExampleCriteria.andPidEqualTo(pid);
            mallCategory1 = mallCategoryMapper.selectByPrimaryKey(pid);
        }
        List<MallCategory> mallCategories = mallCategoryMapper.selectByExample(mallCategoryExample);
        mallCategory1.setName("全部");
        mallCategories.add(0,mallCategory1);
        wxCategoryVO.setBrotherCategory(mallCategories);

        return wxCategoryVO;
    }

}
